package com.nondo.rdp.auth.endpoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.auth.model.UserBeanInner;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.PropertiesTools;
import com.nondo.rdp.core.domain.Rules;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.core.service.RulesService;

@RestController
@RequestMapping(value = "/api/sys/invite")
public class InviteEndpoint {

    @Autowired
    private UserService userService;
    
    @Autowired
    private RulesService rulesService;
    
    private String appLogoUrl = PropertiesTools.getPropertyValue("app.logo_url");

    /**
     * 获取邀请注册链接
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "inviteUrl")
    public View<Map<String,Object>> inviteUrl(HttpServletRequest request, HttpServletResponse response) {
		View<Map<String, Object>> view = new View<Map<String, Object>>(20000,"获取URL成功");
    	User user = UserUtils.getUser(request, response);
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	resultMap.put("registUrl", "wap/app/activity/dynamic/invite/register");
    	resultMap.put("inviterId", user.getId());
    	resultMap.put("registImg",appLogoUrl);
    	view.setData(resultMap);
    	return view;
    }

    /**
     * 邀请用户列表
     *
     * @param query
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/list")
    public View<Page<UserBeanInner>> userList(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<User> query) {
    	User loginUser = UserUtils.getUser(request, response);
    	
        User user = new User();
        user.setInviterId(loginUser.getId());
        
        View<Page<UserBeanInner>> view = View.ofOk();

        Page<User> page = userService.findAll(new Page<User>(query.getPage(), query.getSize()), user);
        Page<UserBeanInner> pageResult = page.convertPage(new ConvertCall<User, Page<UserBeanInner>>(){

			@Override
			public Page<UserBeanInner> execute(Page<User> srcPage) {
				Page<UserBeanInner> userQueryPage = new Page<UserBeanInner>(srcPage.getPageNumber(),srcPage.getPageSize(),srcPage.getTotalPages(),srcPage.getTotalElements());
				List<UserBeanInner> userQueryList = new ArrayList<UserBeanInner>();
				List<User> userList = srcPage.getContent();
				for(User user : userList) {
					UserBeanInner courseQuery = UserBeanInner.covert2Bean(user);
					userQueryList.add(courseQuery);
				}
				userQueryPage.setContent(userQueryList);
				return userQueryPage;
			}
        	
        });
        
        view.setData(pageResult);
        return view;
    }

    /**
     * 邀请记录列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/record/list")
    public View<List<String>> recordList(HttpServletRequest request, HttpServletResponse response) {
    	String description = "";
        View<List<String>> view = View.ofOk();
        List<String> recordList = new ArrayList<String>();
        for(int i=0; i<30; i++){
        	String random = CommUtil.randomInt(6); 
        	String nickName = "菜鸟" + random;
        	if(Integer.parseInt(random)%2==0){
        		description = nickName + "获得一年VIP";
        	}else{
        		Rules rules = this.rulesService.findByType("send_point_invite_register");
        		description = nickName + "获得500课程积分";
        		if(rules!=null){
        			description = nickName + "获得"+rules.getValue()+"课程积分";
        		}
        	}
        	recordList.add(description);
        }
        view.setData(recordList);
        return view;
    }

}
